/**
 * Project Name:九色鹿
 * File Name:CustomerSecurityConfig.java
 * Package Name:com.fxp.project.jiuselu.web.config
 * Date:2021-08-29 12:14 下午
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.web.config;

import com.fxp.project.jiuselu.security.component.JwtAuthenticationTokenFilter;
import com.fxp.project.jiuselu.security.component.RestAuthenticationEntryPoint;
import com.fxp.project.jiuselu.security.component.RestfulAccessDeniedHandler;
import com.fxp.project.jiuselu.security.config.SecurityConfig;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.DigestUtils;

/**
 * ClassName: CustomerSecurityConfig<br/>
 * Function: API接口的权限配置<br/>
 * Date: 2021-08-29 12:14 下午<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK 1.8
 */
@Log4j2
@Configuration
@EnableWebSecurity
public class CustomerSecurityConfig extends SecurityConfig {
    @Autowired
    UserDetailsService userDetailsService;
    @Autowired
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Autowired
    RestfulAccessDeniedHandler restfulAccessDeniedHandler;
    @Autowired
    RestAuthenticationEntryPoint restAuthenticationEntryPoint;

    @Override
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        //校验用户
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    /**
     * passwordEncoder: 创建密码对象<br/>
     *
     * @return
     * @author Jason.Wang
     * @createTime 2021/3/19 22:52
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new PasswordEncoder() {
            //对密码进行加密
            @Override
            public String encode(CharSequence charSequence) {
                log.debug(charSequence.toString());
                return DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
            }

            //对密码进行判断匹配
            @Override
            public boolean matches(CharSequence charSequence, String s) {
                //如果当前的用户没有设置相关的密码等信息，记得配置的加密信息要处理好这个地方
                //String encode = DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
                String encode = charSequence.toString();
                boolean res = s.equals(encode);
                return res;
            }
        };
    }
}
